// 本文件对LpPacket的相关功能进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/16 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const benchmark = require('benchmark')
const MINPacket = require('./MINPacket');
const {Signature} = require("../component/Signature");
const {createSignatureInfoBySignatureTypeValue} = require("../component/SignatureInfo");
const {SignatureAlgorithm} = require("../minsecurity/common");
const {createCustomIdentifierByString} = require("../component/IdentifierWrapper");
const {createCommonIdentifierByString} = require("../component/IdentifierWrapper");
const {createSignatureValueByByteArray} = require("../component/SignatureValue");
const {createBlockByBuffer} = require("../encoding/Block");
const {CPacket} = require("./CPacket");
const {Encoder, MaxPacketSize} = require('../encoding').Encoder
const {createIdentifierByString} = require("../component/Identifier");

// 测试WireEncode
let packet = new MINPacket()
let wrapper = createCommonIdentifierByString('/min/pku/sz')
let wrapper2 = createCustomIdentifierByString(105, '/install')
packet.IdentifierField.setIdentifier(0, wrapper)
packet.IdentifierField.setIdentifier(1, wrapper2)

let buf = new Uint8Array([1,2,3,4,5,6,7,8])
let got = createSignatureValueByByteArray(buf)
let buf1 = new Uint8Array([1,2,3,4,5,6,7,9])
let got1 = createSignatureValueByByteArray(buf1)

let value = SignatureAlgorithm.SM2WithSM3
let info = createSignatureInfoBySignatureTypeValue(value)

let sig = new Signature()
sig.SigInfo = info
sig.SigValue = got

let sig2 = new Signature()
sig2.SigInfo = info
sig2.SigValue = got1

packet.SignatureField.addSignature(sig)
packet.SignatureField.addSignature(sig2)

packet.packetType = 3
let enc = new Encoder()
const suite1 = new benchmark.Suite()
suite1.add('wireEncode', ()=>{
    enc.encoderReset(MaxPacketSize, 0)
    packet.wireEncode(enc)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireEncode finished')
}).run()

enc.encoderReset(MaxPacketSize, 0)
packet.wireEncode(enc)
buf = enc.getBuffer()
let packet2 = new MINPacket()
let block = createBlockByBuffer(buf, false)
const suite3 = new benchmark.Suite()
suite3.add('wireDecode', ()=>{
    packet2.wireDecode(block)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireDecode finished')
}).run()
